package cn.eonshine.cuba.inf.modular.basic.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.Mapping;
import cn.eonshine.cuba.inf.modular.basic.entity.Oiltank;
import cn.eonshine.cuba.inf.modular.basic.mapper.OiltankMapper;
import cn.eonshine.cuba.inf.modular.basic.service.IOiltankService;
import cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService;

/**
 * <p>
 * Oil Tank（油罐表） 服务实现类
 * </p>
 *
 * @author Alan
 * @since 2018-07-28
 */
@Service
public class OiltankServiceImpl extends AbstractCommonService<OiltankMapper, Oiltank> implements IOiltankService {

	@Override
	public int transportDataHandle(Oiltank oiltank, int count) throws Exception {
		String oilDepotId = oiltank.getOilDepotId();
		String oilTankCode = oiltank.getOilTankCode();
		String oilId = oiltank.getOilId();
		Date dataInputDatetime = oiltank.getDataInputDatetime();

		QueryWrapper<Oiltank> entity = new QueryWrapper<Oiltank>();
		//entity.where("OIL_DEPOT_ID={0} and data_input_datetime={1} and OIL_TANK_CODE={2} and oil_id={3}", oilDepotId, dataInputDatetime, oilTankCode, oilId);
		entity.lambda().eq(Oiltank::getOilDepotId, oilDepotId);
		entity.lambda().eq(Oiltank::getDataInputDatetime, dataInputDatetime);
		entity.lambda().eq(Oiltank::getOilTankCode, oilTankCode);
		entity.lambda().eq(Oiltank::getOilId, oilId);
		
		if(this.isUpdate==0){
			boolean isDelete = remove(entity);
			if (isDelete) {
				count++;
			}
		}else {
			boolean isupdate = update(oiltank,entity);
			if (isupdate) {
				count++;
			}
		}
		return count;
	}

	@Override
	public void mappingHandle(Oiltank oiltank) throws Exception {
		Mapping map = new Mapping();
		QueryWrapper<Mapping> entitys = new QueryWrapper<Mapping>();
		map.setLsmsId(oiltank.getId());
		map.setDmz1(oiltank.getOilTankCode());
		map.setDmz2(oiltank.getOilDepotId());
		map.setDmz3(oiltank.getOilId());
		map.setName(oiltank.getOilTankName());
		map.setDataType(InfStatus.DMZ_MAPPING_OIL_TANKER);
		entitys.eq("lsms_id", oiltank.getId());
		Mapping maps = mappingService.getOne(entitys);
		if (maps == null) {
			mappingService.save(map);
		}else {
			mappingService.update(map, entitys);
		}
	}


	/**   
	 * <p>Title: customerQueryWrapper</p>   
	 * <p>Description: </p>   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#customerQueryWrapper()   
	 * @author: Alan
	 * @date:   2019-03-13 11:29:19
	 */
	@Override
	public QueryWrapper<Oiltank> customerQueryWrapper() {
		QueryWrapper<Oiltank> entity = new QueryWrapper<Oiltank>();
		entity.eq("lsms_update", InfStatus.LSMS_UPDATE_EXTRACT_NO);
		entity.orderByAsc("oil_tank_code");
		return entity;
	}

	@Override
	public boolean isUseCutomerSelectSQL() {
		return true;
	}

}
